﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.AppCode.Lib;

namespace mojoPortal.Web.RS.Modules.MLS.Controls
{
    public partial class PublicMLSConfirmControl : RSBaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SetControlEnabledState(false);
            }
        }
        public void btnSubmitUserEmail_Click(object sender, EventArgs e)
        {
            int mlsid = hfMLSID.Value == "" ? 0 : int.Parse(hfMLSID.Value);
            string mlstoken = hfMLSToken.Value;
            string useremail = txtUserEmail.Text;
            if (!LoadMLSData(mlsid, mlstoken, useremail))
            {
                lblMessage.Text = "Sorry.  Your user email and MLS entry could not be confirmed.";
                lblMessage.ForeColor = System.Drawing.Color.DarkRed;
                pnlMLSDetail.Visible = false;
                pnlUserEmail.Visible = true;
            }
            else
            {
                lblMessage.Text = "Please confirm your MLS information, save any changes and select Submit.";
                lblMessage.ForeColor = System.Drawing.Color.Black;
                pnlUserEmail.Visible = false;
                pnlMLSDetail.Visible = true;
            }
        }
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            string prevurl = string.Empty;
            if (Session["PreviousURL"] != null)
                prevurl = Session["PreviousURL"].ToString();
            else
                prevurl = Request.UrlReferrer.ToString();
            Response.Redirect(prevurl);
        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                SetControlEnabledState(true);
                btnEdit.Visible = false;
                btnSave.Visible = true;
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {

        }
        public void SetControlEnabledState(bool bstate)
        {
            RSCommonFunctions.SetControlsEnabledState(this, bstate);
        }
        
        public bool LoadMLSData(int mlsid, string mlstoken, string useremail)
        {
            bool bHasValue = false;
            hfMLSID.Value = mlsid.ToString();
            hfMLSToken.Value = mlstoken;
            if (mlsid > 0 && mlstoken.Length > 0 && useremail.Length > 0)
            {
                RSDataLookupLoader.LoadPublicTypeDDL("PROPERTY_CAT", ddlPropertyType);
                RSDataLookupLoader.LoadPublicTypeDDL("MLS_CAT", ddlMLSType);
                RSDataLookupLoader.LoadPublicTypeDDL("HOATERM_CAT", ddlHOATerm);
                RSDataLookupLoader.LoadPublicStatusDDL("MLS_CAT", ddlMLSStatus);
                RSDataLookupLoader.LoadIntegerGenDDL(ddlBedNum, 0, 20);
                RSDataLookupLoader.LoadDigitGenDDL(ddlBathNum, 0, 10);
                

                String sql = "EXEC rs_uspGetPublicMLSByID @MLSID";
                SqlCommand myCommand;
                SqlParameter param;
                SqlConnection myConnection;
                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ReadyServiceConnectionStringMain"].ToString());
                myCommand = new SqlCommand(sql, myConnection);
                SqlDataReader myDataReader;

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@MLSID";
                param.Value = mlsid;
                myCommand.Parameters.Add(param);

                myConnection.Open();
                myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                if (myDataReader.HasRows)
                {
                    myDataReader.Read();
                    txtMLSID.Text = myDataReader["MLSID"].ToString();
                    txtSalePrice.Text = myDataReader["SalePrice"].ToString();
                    txtRentPrice.Text = myDataReader["RentPrice"].ToString();
                    txtHOAFee.Text = myDataReader["HOAFee"].ToString();
                    txtTax.Text = myDataReader["Tax"].ToString();
                    txtStreet.Text = myDataReader["Street"].ToString();
                    txtCity.Text = myDataReader["City"].ToString();
                    txtCounty.Text = myDataReader["County"].ToString();
                    txtState.Text = myDataReader["State"].ToString();
                    txtCountry.Text = myDataReader["Country"].ToString();
                    txtZip.Text = myDataReader["Zip"].ToString();
                    txtSubdivisionName.Text = myDataReader["SubdivisionName"].ToString();
                    txtSqFt.Text = myDataReader["SqFt"].ToString();
                    ddlBedNum.SelectedValue = myDataReader["BedNum"].ToString();
                    ddlBathNum.SelectedValue = myDataReader["BathNum"].ToString();
                    txtYearBuilt.Text = myDataReader["YearBuilt"].ToString();
                    txtDescription.Text = myDataReader["Description"].ToString();
                    txtElementarySchool.Text = myDataReader["ElementarySchool"].ToString();
                    txtMiddleSchool.Text = myDataReader["MiddleSchool"].ToString();
                    txtHighSchool.Text = myDataReader["HighSchool"].ToString();

                    if (myDataReader["Status"] != DBNull.Value)
                    {
                        string mlsstatus = myDataReader["Status"].ToString();
                        ddlMLSStatus.SelectedValue = mlsstatus;
                    }
                    if (myDataReader["IsOccupied"] != DBNull.Value)
                    {
                        bool isOccupied = Convert.ToBoolean(myDataReader["IsOccupied"]);
                        chkIsOccupied.Checked = isOccupied;
                    }
                    if (myDataReader["PropertyTypeID"] != DBNull.Value)
                    {
                        string propTypeId = myDataReader["PropertyTypeID"].ToString();
                        ddlPropertyType.SelectedValue = propTypeId;
                    }
                    if (myDataReader["HOATerm"] != DBNull.Value)
                    {
                        string hoaterm = myDataReader["HOATerm"].ToString();
                        ddlHOATerm.SelectedValue = hoaterm;
                    }
                    if (myDataReader["MLSType"] != DBNull.Value)
                    {
                        string mlstype = myDataReader["MLSType"].ToString();
                        ddlMLSType.SelectedValue = mlstype;
                    }
                    if (myDataReader["PhotoPath"] != DBNull.Value)
                    {
                        imgPropertyPhoto.ImageUrl = myDataReader["PhotoPath"].ToString();
                    }
                    if (myDataReader["IsApproved"] != DBNull.Value)
                    {
                        hfIsApproved.Value = myDataReader["IsApproved"].ToString();
                    }

                    btnEdit.Visible = true;
                    bHasValue = true;
                    myConnection.Close();
                }
            }
            return bHasValue;
        }
        
        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            int mlsid = txtMLSID.Text == "" ? 0 : int.Parse(txtMLSID.Text);
            string mlstoken = hfMLSToken.Value;
            float BedNum = float.Parse(ddlBedNum.SelectedValue);
            float BathNum = float.Parse(ddlBathNum.SelectedValue);
            int SqFt = txtSqFt.Text == "" ? 0 : int.Parse(txtSqFt.Text);
            int YearBuilt = txtYearBuilt.Text == "" ? 0 : int.Parse(txtYearBuilt.Text);
            int PropertyTypeID = int.Parse(ddlPropertyType.SelectedValue);
            string Street = txtStreet.Text;
            string City = txtCity.Text;
            string State = txtState.Text;
            string Zip = txtZip.Text;
            string County = txtCounty.Text;
            string Country = txtCountry.Text;
            string SubdivisionName = txtSubdivisionName.Text;
            string Description = txtDescription.Text;
            string ElementarySchool = txtElementarySchool.Text;
            string MiddleSchool = txtMiddleSchool.Text;
            string HighSchool = txtHighSchool.Text;

            int tax = txtTax.Text == "" ? 0 : int.Parse(txtTax.Text);
            int saleprice = txtSalePrice.Text == "" ? 0 : int.Parse(txtSalePrice.Text);
            int rentprice = txtRentPrice.Text == "" ? 0 : int.Parse(txtRentPrice.Text);
            int hoafee = txtHOAFee.Text == "" ? 0 : int.Parse(txtHOAFee.Text);
            int hoaterm = int.Parse(ddlHOATerm.SelectedValue);
            int mlstype = int.Parse(ddlMLSType.SelectedValue);
            int status = int.Parse(ddlMLSStatus.SelectedValue);
            bool isoccupied = chkIsOccupied.Checked;
            bool isconfirmed = true;
            bool isapproved = hfIsApproved.Value == "" ? false : bool.Parse(hfIsApproved.Value);

            SqlCommand sqlCmd = new SqlCommand();
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@BedNum", SqlDbType.Float, 0, ParameterDirection.Input, BedNum);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@BathNum", SqlDbType.Float, 0, ParameterDirection.Input, BathNum);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@SqFt", SqlDbType.Int, 0, ParameterDirection.Input, SqFt);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@YearBuilt", SqlDbType.Int, 0, ParameterDirection.Input, YearBuilt);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Street", SqlDbType.NVarChar, 250, ParameterDirection.Input, Street);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@City", SqlDbType.NVarChar, 50, ParameterDirection.Input, City);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@State", SqlDbType.NVarChar, 50, ParameterDirection.Input, State);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@County", SqlDbType.NVarChar, 50, ParameterDirection.Input, County);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Country", SqlDbType.NVarChar, 50, ParameterDirection.Input, Country);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Zip", SqlDbType.NVarChar, 10, ParameterDirection.Input, Zip);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@SubdivisionName", SqlDbType.NVarChar, 250, ParameterDirection.Input, SubdivisionName);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Description", SqlDbType.NVarChar, 250, ParameterDirection.Input, Description);

            SQLHelper.AddParamToSQLCmd(sqlCmd, "@MLSType", SqlDbType.Int, 0, ParameterDirection.Input, mlstype);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@HOAFee", SqlDbType.Int, 0, ParameterDirection.Input, hoafee);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@HOATerm", SqlDbType.Int, 0, ParameterDirection.Input, hoaterm);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Tax", SqlDbType.Int, 0, ParameterDirection.Input, tax);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@SalePrice", SqlDbType.Int, 0, ParameterDirection.Input, saleprice);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@RentPrice", SqlDbType.Int, 0, ParameterDirection.Input, rentprice);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@MLSToken", SqlDbType.NVarChar, 50, ParameterDirection.Input, mlstoken);

            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ElementarySchool", SqlDbType.NVarChar, 250, ParameterDirection.Input, ElementarySchool);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@MiddleSchool", SqlDbType.NVarChar, 250, ParameterDirection.Input, MiddleSchool);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@HighSchool", SqlDbType.NVarChar, 250, ParameterDirection.Input, HighSchool);

            SQLHelper.AddParamToSQLCmd(sqlCmd, "@IsOccupied", SqlDbType.Bit, 0, ParameterDirection.Input, isoccupied);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@IsConfirmed", SqlDbType.Bit, 0, ParameterDirection.Input, isconfirmed);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@IsApproved", SqlDbType.Bit, 0, ParameterDirection.Input, isapproved);

            SQLHelper.AddParamToSQLCmd(sqlCmd, "@PropertyTypeID", SqlDbType.Int, 0, ParameterDirection.Input, PropertyTypeID);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@MLSID", SqlDbType.Int, 0, ParameterDirection.Input, mlsid);
            SQLHelper.SetCommandType(sqlCmd, CommandType.StoredProcedure, "rs_uspSavePublicMLS");
            SQLHelper.ExecuteScalarCmd_ReadyServiceMain(sqlCmd);

            int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
            if (returnValue > 0)
            {
                if (FileUpload1.HasFile)
                {
                    string folder = ConfigurationManager.AppSettings["ReadyServiceMLSPhotoPath"].ToString();
                    string fileLocalPath = Server.MapPath(folder);
                    string photoname = FileUpload1.FileName.Replace(" ", "_");
                    string photopath = folder + returnValue + "_" + photoname;
                    string filename_localpath = fileLocalPath + returnValue + "_" + photoname;
                    FileUpload1.SaveAs(filename_localpath);

                    mlsid = returnValue;
                    string SQL = "UPDATE rs_tblMLS SET PhotoPath=@PhotoPath WHERE MLSID=@MLSID";
                    sqlCmd = new SqlCommand();
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@PhotoPath", SqlDbType.NVarChar, 150, ParameterDirection.Input, photopath);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@MLSID", SqlDbType.Int, 0, ParameterDirection.Input, mlsid);
                    SQLHelper.SetCommandType(sqlCmd, CommandType.Text, SQL);
                    SQLHelper.ExecuteScalarCmd_ReadyServiceMain(sqlCmd);
                }

                string prevurl = string.Empty;
                if (Session["PreviousURL"] != null)
                    prevurl = Session["PreviousURL"].ToString();
                else
                    prevurl = Request.UrlReferrer.ToString();
                ucModalActionMessageControl.ShowModal("Property Successfully Saved!", prevurl, true);
                btnSave.Enabled = false;
            }
        }
    }
}